package com.blb.bookms.controller;

import com.blb.bookms.config.SpringConfig;
import com.blb.bookms.entity.BookTypeTreeNode;
import com.blb.bookms.service.IBookService;
import com.blb.bookms.service.IBookTypeService;
import com.blb.bookms.service.impl.BookTypeServiceImpl;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/bookType.do")
public class BookTypeServlet extends BaseServlet {

    @Autowired
    private IBookTypeService bookTypeService = SpringConfig.getBean(IBookTypeService.class);

    public void findBookTypes(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=UTF8");
        //查询所有类型节点
        List<BookTypeTreeNode> allBookType = bookTypeService.findAllBookType();
        //把集合转换JSON字符串
        String json = new Gson().toJson(allBookType);
        //把字符串发送给客户端
        resp.getWriter().print(json);
    }

    public void toBookSave(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        //查询所有类型节点
        List<BookTypeTreeNode> allBookType = bookTypeService.findAllBookType();
        req.setAttribute("bookTypes",allBookType);
        req.getRequestDispatcher("book_save.jsp").forward(req,resp);
    }
}
